package com.coin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coin.domain.WebConfig;
import com.coin.service.WebConfigService;
import com.coin.mapper.WebConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 李力
 * @description 针对表【web_config(网站配置信息)】的数据库操作Service实现
 * @createDate 2025-05-24 17:08:03
 */
@Service
public class WebConfigServiceImpl extends ServiceImpl<WebConfigMapper, WebConfig>
        implements WebConfigService {

    /**
     * @Description: 分页查询
     * @Author: 李 力
     * @Date: 2025/6/3
     * @Param: page name type
     * @return: R
     */
    @Override
    public Page<WebConfig> findByPage(Page<WebConfig> page, String name, String type) {
        LambdaQueryWrapper<WebConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.like(StrUtil.isNotEmpty(name), WebConfig::getName, name)
                .like(StrUtil.isNotEmpty(type), WebConfig::getType, type);
        return page(page, queryWrapper);
    }

    /**
     * @Description: 新增配置
     * @Author: 李 力
     * @Date: 2025/6/3
     * @Param: webConfig
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWebConfig(WebConfig webConfig) {
        if (webConfig == null) {
            throw new RuntimeException("参数为空");
        }
        return save(webConfig);
    }

    /**
     * @Description: 修改配置
     * @Author: 李 力
     * @Date: 2025/6/3
     * @Param: webConfig
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWebConfig(WebConfig webConfig) {
        if (webConfig == null) {
            throw new RuntimeException("参数为空");
        }
        return updateById(webConfig);
    }

    /**
     * @Description: 删除配置
     * @Author: 李 力
     * @Date: 2025/6/3
     * @Param: ids
     * @return: R
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeWebConfig(String[] ids) {
        if (ids == null || ids.length == 0) {
            throw new RuntimeException("参数为空");
        }
        return removeByIds(Arrays.asList(ids));
    }

    /**
     * @Description: 获取PC端的banner图
     * @Author: 李 力
     * @Date: 2025/6/20
     * @Param:
     * @return: R
     */
    @Override
    public List<WebConfig> banners() {
        LambdaQueryWrapper<WebConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = queryWrapper.eq(WebConfig::getType, "WEB_BANNER")
                .eq(WebConfig::getStatus, 1)
                .orderByAsc(WebConfig::getSort);
        return list(queryWrapper);
    }


}




